#include <iostream>
#include <unordered_map>
#include <queue>
#include <vector>
#include <utility>

using std::cout;
using std::endl;
using std::vector;
using std::queue;
using std::unordered_map;
using std::priority_queue;

struct Compare{
    bool operator()(const std::pair<int, int> &lhs, const std::pair<int, int> &rhs) const
    {
        return rhs.second < lhs.second;
    }
};

// 求出重复次数最小的前k个
void resolve(vector<int> &vec)
{
    unordered_map<int, int> map;
    priority_queue<std::pair<int, int>, vector<std::pair<int, int>>, Compare> que;
    for(int i: vec){
        map[i]++;
    }

    int i = 0;
    for(auto [key, val]: map){
        if(++i == 5) break;
            que.push({key, val});
    }

    for(auto iter = map.begin(); iter != map.end(); iter++){
        if(--i <= 0) {
            if(iter->second > que.top().second){
                que.pop();
                que.push(*iter);
            }
        }
    }

    while(!que.empty()){
        cout << "key: " << que.top().first << ", val: " << que.top().second << endl;
        que.pop();
    }
    for(auto [key, val]: map){
        cout << "map-k: " << key << ", map-v: " << val << endl;
    }
}

int main(int args, char *argv[])
{
    vector<int> vec;
    for(int i{}; i < 50; i++){
        vec.push_back(rand() % 10);
    }
    resolve(vec);
    return 0;
}